import {concat} from 'ramda'

function ModalAction() {
    this.modals = [];
}

ModalAction.prototype.register = function (modal) {
    this.modals = concat([modal], this.modals)
}

ModalAction.prototype.getModalProps = function (modalName) {
    const modal = modalActions.modals.find(x => x.name == modalName)
    return (modal || {}).props || {}
}

ModalAction.prototype.findModal = function (modalName) {
    return modalActions.modals.find(x => x.name == modalName)
}

const modalActions = new ModalAction()

const getModalProps = modalActions.getModalProps

function Modal(modal) {
    this.name = modal
    this.props = {};
    modalActions.register(this)
}

Modal.prototype.upsertProp = function (props) {
    this.props = {...this.props, ...props}
}

const setNewModal = (name) => new Modal(name)
const upsertProp = ({name, props}) => {
    const modal = modalActions.findModal(name)
    if (modal) {
        modal.upsertProp(props)
    }
}


export {upsertProp, getModalProps, setNewModal}